import { _decorator, Button, Component, Node, Vec3 } from 'cc';
import { DataToUiEvent } from '../Event/DataToUiEvent';
import { DB } from '../Data/DB';
import { DisObjUtil } from '../Utils/DisObjUtil';
import { SoundMgr } from '../Managers/SoundMgr';
const { ccclass, property } = _decorator;

@ccclass('BasePanel')
export class BasePanel extends Component {
    // private listeners:{[key:string]:{[key:number]:any[]}};
    /**
     * 
     */
    private _evtList:any[] = [];
    private _lastSiblingIndex:number = 0;

    public constructor(){
        super();
    }

    onLoad():void{
        //TODO
    }

    public show():BasePanel{
        this.setVisible(true);
        return this;
    }

    public hide():BasePanel{
        this.setVisible(false);
        return this;
    }

    public setVisible(value:boolean):void{
        this.node.active = value;
    }
    /**
     * 自动销毁监听
     * @param evetName 
     * @param fn 
     * @param tagToRemove 
     */
    protected autoDisposeListener(eventName:string, fn:Function, tagToRemove:any=null):void{
        var handleId = DB.getInstnace().addEventListener(eventName, fn, tagToRemove);
        this._evtList.push([eventName, handleId]);
    }
    /**
     * 
     * @param e 
     */
    protected dispatchEvent(e:DataToUiEvent):void{
        DB.getInstnace().dispatchEvent(e);
    }

    /**
     * button 绑定 Click
     * this.readybtn.node.on(Button.EventType.CLICK, this.onTestBtnClick, this);
     * @param btn 
     * @param fn 
     */
    public bindBtnClick(btn:Button, fn:Function):void{
        btn.node.on(Button.EventType.CLICK, fn, this);
    }

    public bindBtnTouchStart(btn:Button, fn:Function):void{
        btn.node.on(Node.EventType.TOUCH_START, fn, this);
    }

    public bindBtnTouchEnd(btn:Button, fn:Function):void{
        btn.node.on(Node.EventType.TOUCH_END, fn, this);
    }

    /**
     * 销毁
     */
    public Dispose():void{
        //移除监听
        for(var idx=0; idx<this._evtList.length;idx++){
            var arr = this._evtList[idx];
            DB.getInstnace().gEvent.removeEventListenersByName(arr[0], arr[1]);
        }
        DisObjUtil.removeMe(this.node);
    }

    public setScale(value:number):BasePanel{
        this.node.setScale(value, value);
        return this;
    }

    /**
     * 
     * @param value 
     */
    public setSiblingIndex(value:number):void{
        this._lastSiblingIndex = value;
        this.node.setSiblingIndex(value);
    }
    /**
     * 
     */
    public get lastSiblingIndex():number{
        return this._lastSiblingIndex;
    }
    /**
     * 
     * @returns 
     */
    public getSiblingIndex():number{
        return this.node.getSiblingIndex();
    }
    /**
     * 设置位置
     * @param x 
     * @param y 
     */
    public setPos(x:number, y:number, z:number=0):void{
        // console.log("[BaseCard] setPos::[" + x + "," + y + "]");
        this.node.setPosition(x, y, z);
    }
    /**
     * 设置 x 坐标
     * @param x 
     */
    public setPosX(x:number):void{
        this.setPos(x, this.y);
    }

    /**
     * 设置 y 坐标
     * @param y 
     */
    public setPosY(y:number):void{
        this.setPos(this.x, y);
    }

    /**
     * x 增量
     * @param value 
     */
    public addPosX(value:number):void{
        this.setPosX(this.x + value);
    }

    /**
     * y 增量
     * @param value 
     */
    public addPosY(value:number):void{
        this.setPosY(this.y + value);
    }

    /**
     * x 坐标
     */
    public get x() : number {
        return this.node.getPosition().x;
    }
    /**
     * y 坐标
     */
    public get y() : number {
        return this.node.getPosition().y;
    }

    public getPos():Vec3{
        return this.node.getPosition();
    }
}

